/*
	Copyright (c) 2008, Grant Jones <grant@podtronics.com>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or
	without modification, are permitted provided that the following
	conditions are met:

		Redistributions of source code must retain the above copyright
		notice, this list of conditions and the following disclaimer.
		
		Redistributions in binary form must reproduce the above copyright
		notice, this list of conditions and the following disclaimer in
		the documentation and/or other materials provided with the
		distribution.
		
		Neither the name of the gxjones.com nor the names of its
		contributors may be used to endorse or promote products derived
		from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
	OF THE POSSIBILITY OF SUCH DAMAGE.
	
	http://www.opensource.org/licenses/bsd-license.php
*/

/*
 *  edb_msg_utils.h
 *  scatterd
 *
 *  Created by Grant Jones on 2/25/08.
 *
 */

#ifndef EDB_MSG_UTILS_H
#define EDB_MSG_UTILS_H
/*!
	@function edb_strdup
	@abstract duplicates a string
	@discussion allocates sufficient memory for a copy of the string
		s1, does the copy, adds a null-terminator and returns a
		pointer to it.
	@param pcs the program context used to obtain a scratch mempool
	@param s1 a string; embedded nulls are allowed but may have
		undesireable results
	@param s1_len length of a string
	@param debug_name an optional name which could be used for mempool
		memory debugging, may be NULL
	@result a newly allocated string of length s1+len+1 containing
		s1 contents. returns NULL if allocation is not possible
*/
char *edb_strdup( mempool_t *pool, const char *s1, int s1_len, const char *debug_name );


int edb_malloc_result_attr( mempool_t *pool, edb_net_item_attr_t *attr, const char *key, size_t key_sz, size_t value_sz );
edb_net_item_t *edb_add_result( cmapStruct *result_items, size_t attr_count );
/*!
	@function edb_term_result_attr
	@abstract allocates appropriately sized memory from pool for a single net item attribute
	@discussion
	@param pool
	@param attr
	@param key null-terminated string
	@param value null-terminated string
	@result attr is populated with allocated memory and filled values
*/
int edb_term_result_attr( mempool_t *pool, edb_net_item_attr_t *attr, const char *key, const char *value_fmt, ... );


void edbDestroyToken( Token *t );
Token *edbResolveVariable( edbParseContext *ctx, Token *var_name );
Token *edbValueFromID( edbParseContext *ctx, Token *t );
Token *edbDequote( edbParseContext *ctx, Token *t );

/*!
	@function edbCreateAttrValueFromPool
	@abstract allocates an AttrValue structure populating it with values
	@discussion indirectly used by the parser, directly used by the domain manager
	@param pool
	@param key
	@param value
	@result a newly allocated AttrValue structure and data from the pool
		NULL on error
*/
AttrValue *edbCreateAttrValueFromPool( mempool_t *pool, Token *key, Token *value );
AttrValue *edbCreateAttrValue( edbParseContext *ctx, Token *key, Token *value );
AttrValue *edbCreateAttrValueFromVariable( edbParseContext *ctx, Token *key, Token *var_name );
AttrValue *edbCreateAttrValueFromInteger( edbParseContext *ctx, Token *key, Token *int_value );
AttrValue *edbCreateAttrValueCStr( edbParseContext *ctx, const char *key, Token *value );
void edbDestroyAttrValue( AttrValue *v );

/* Note: AttrLists are actually strmaps and strmaps are actually wrappers for cmaps */
AttrList *edbAppendAttrListFromPool( mempool_t *pool, AttrList *l, AttrValue *a );
AttrList *edbAppendAttrList( edbParseContext *ctx, AttrList *l, AttrValue *a );
void edbAttrListDestroy( AttrList *l );

#endif


